#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      sp41822
#
# Created:     21/03/2013
# Copyright:   (c) sp41822 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
from HTMLParser import HTMLParser
import urllib, os, zipfile, sys, urllib2
from PyQt4 import QtGui, QtCore
import osmand_ui

filespath=""
urlosmand="download.osmand.net"
def main():
    if not len(sys.argv) > 1 or len(sys.argv[1])<1:
        exit()
    else:
        gogogo(sys.argv[1])


def gogogo(query,ui=None,dt=None,tick=None):
    print("start working")
    clean()
    a=parseZip(query)
    #a.feed(urllib.urlopen("http://code.google.com/p/osmand/downloads/list?can=2&q="+query).read())
    a.feed(urllib.urlopen("http://"+urlosmand+"/list.php").read())
    for zip in a.getZips():
        dls = dlFiles()
        dls.getFile(zip, ui, dt, tick)
    dec = decompFiles(a.getZips())
    dec.concat()
    dec.decomp()
    clean()
    if(ui):
        ui.pct("ops ok")

def clean():
    global filespath
    filespath=os.getcwd()+"/tmpOSMandFiles/"
    if not os.path.exists(filespath):
        os.makedirs(filespath)
    for file in os.listdir(filespath):
        if(os.path.isfile(filespath+file) and not file.endswith(".obf")):
            os.remove(filespath+file)

class parseZip(HTMLParser):
    """
    liste de tableaux
    le premier designe le fichier cible decompresse
    le deuxieme designe le fichier source
    """
    def __init__(self, query=""):
        HTMLParser.__init__(self)
        self.zips=[]
        self.query=query
    def getZips(self):
        self.zips.sort()
        return self.zips
    def handle_starttag(self, tag, attrs):
        if tag == "a":
            for i in attrs:
                print i
                if(i[0]=='href' and i[1].startswith("/") and i[1].endswith(".zip") and self.query in i[1]):
                    filename=i[1].split('file=')[-1]
                    self.zips.append(["http://"+urlosmand+i[1],filename])

class dlFiles(object):
    def __init__(self):
        pass
    def getFile(self, url, ui, dt,tick):
        print("will retrieve ", url[0],"in",filespath)
        try:
            #u = urllib.urlretrieve(url[0],filespath+url[0].split('/')[-1])
            u = urllib2.urlopen(url[0])
            size=int(u.info().getheader("Content-Length"))
            print size
            f=open(filespath+url[0].split('file=')[-1],'wb')
            idx=0
            chunkSize=102400
            while True:
                chunk = u.read(chunkSize)
                idx=idx+chunkSize
                if(ui):
                    tick.emit(int(idx*100/size))
                    #ui.pbar(int(idx*100/size))
                if not chunk:
                    print("f will be closed")
                    f.close()
                    break
                else:
                    f.write(chunk)

            u.close()
        except:
            print sys.exc_info()[0]
            exit()

class decompFiles(object):
    def __init__(self, files):
        self.files=files
        self.filesAdecomp=[]
    def concat(self):
        """
        boucler sur liste "files"
            si fichier termine par zip ne rien faire et alimenter liste filesAdecomp
            si fichier ne termine pas par zip copier source vers cible
                ajouter fichiers s'il n'existe pas
        """
        for file in self.files:
            if(file[0][-3:]=="zip"):
                self.filesAdecomp.append(file[1])
            else:
                srcFile=filespath+file[0].split("/")[-1:][0]
                dstFile=filespath+file[1]
                print("copy",srcFile,"dans",dstFile)
                s = open(srcFile,'rb')
                d = open(dstFile,'ab')
                d.write(s.read())
                s.close()
                d.close()

                if(file[1] not in self.filesAdecomp):
                    self.filesAdecomp.append(file[1])
        print(self.filesAdecomp)

    def decomp(self):
        """
        si fichier termine par zip alors decomp fichier
        si fichier ne termine pas par zip copier source vers cible
        """
        for file in self.filesAdecomp:
            print("will unzip",filespath+file)
            z = zipfile.ZipFile(filespath+file)
            for f in z.namelist():
                if f.endswith(".obf"):
                    z.extract(f,filespath)
            z.close()


if __name__ == '__main__':
    main()
